import { Component, OnInit } from '@angular/core';

import { Bird } from '../model/bird-model';
import { BirdFlyService } from '../bird-fly/service/bird-fly.service';
import { Observable } from 'rxjs/Rx';
import { subscribeOn } from '_rxjs@5.5.2@rxjs/operator/subscribeOn';
import { Subscription } from '_rxjs@5.5.2@rxjs/Subscription';

@Component({
    selector: 'bird',
    templateUrl: "./bird.component.html",
    styleUrls: ['./bird.component.css']
})

export class BirdComponent implements OnInit {
    currentStyles = {};
    birdStatus: string = 'down';
    obIndex: number;

    constructor(public birdFlyService: BirdFlyService) {
        this.setCurrentStyles();
    }
    ngOnInit() {
        this.currentStyles = this.birdFlyService.initBirdStyle();
        let bird = this.birdFlyService.getBird();
    }
    setCurrentStyles() {
        //渲染间隔
        let time: number = this.birdFlyService.getTime();
        //设置移动速度（正负速度切换期间，每次移动的平均时间，越低则移动越快）
        let timePerMove: number = 300 / time;
        let observer = data => {
            this.birdStatus = this.birdFlyService.getBird().status;
            if(this.birdStatus == "over"){
                this.birdFlyService.cancleOneSubscription(this.obIndex);
            }
            this.currentStyles = this.birdFlyService.getBirdStyle(timePerMove);
        }
        this.obIndex = this.birdFlyService.addObserver(observer);
    }

}